package me.zfz.business.controller.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import me.zfz.base.bean.SmartResponse;
import me.zfz.business.bean.RequestPage;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.VersionSearch;
import me.zfz.business.bean.entity.TNVersion;
import me.zfz.business.constant.enums.VersionType;
import me.zfz.business.controller.base.BaseController;
import me.zfz.business.service.OPService;
import me.zfz.business.service.VersionService;
import me.zfz.base.tag.bean.DelBtn;
import me.zfz.base.tag.bean.EditBtn;
import me.zfz.base.tag.bean.PageParam;
import me.zfz.base.tag.bean.RefreshBtn;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mixsmart.utils.StringUtils;

/**
 * 版本控制器
 *
 * @author author
 */
@Controller
@RequestMapping("/version")
public class VersionController extends BaseController {

    private final String VIER_DIR = baseDir + "/version/";

    @Autowired
    private OPService opServ;
    @Autowired
    private VersionService versionServ;

    /**
     * @param session
     * @param searchParam
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(HttpSession session, VersionSearch searchParam, RequestPage page) {
        ModelAndView modelView = new ModelAndView();
        SmartResponse<Object> smartResp = opServ.getDatas("version_mgr_list", searchParam, page.getStartNum(), page.getPageSize());
        addBtn = new EditBtn("add", "version/add", null, "添加版本信息", "800");
        editBtn = new EditBtn("edit", "version/edit", null, "修改版本信息", "800");
        delBtn = new DelBtn("version/delete", "确定要删除选中的版本信息吗？", "version/list", null, null);
        String url = "version/list";
        refreshBtn = new RefreshBtn(url, null, null);

        pageParam = new PageParam(url, null, page.getPage(), page.getPageSize());
        Map<String, Object> modelMap = modelView.getModelMap();
        modelMap.put("addBtn", addBtn);
        modelMap.put("editBtn", editBtn);
        modelMap.put("delBtn", delBtn);
        modelMap.put("pageParam", pageParam);
        modelMap.put("refreshBtn", refreshBtn);
        modelMap.put("smartResp", smartResp);
        modelMap.put("searchParam", searchParam);

        modelView.setViewName(VIER_DIR + "list");
        return modelView;
    }

    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelView = new ModelAndView();
        modelView.getModelMap().put("version", versionServ.getLastVersionAddOne(VersionType.PC));
        modelView.setViewName(VIER_DIR + "add");
        return modelView;
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public SmartResponse<String> save(HttpSession session, TNVersion version) {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != version && StringUtils.isNotEmpty(version.getVersion())
                && StringUtils.isNotEmpty(version.getDescr())) {
            if (StringUtils.isEmpty(version.getUpdateDate())) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                version.setUpdateDate(formatter.format(new Date()));
            }
            UserInfo userInfo = getUserInfoFromSession(session);
            version.setVersion(version.getVersion().toUpperCase());
            version.setUserId(userInfo.getId());
            version.setDescr(escapeDescr(version.getDescr()));
            version.setType(VersionType.PC.getValue());
            smartResp = versionServ.save(version);
        }
        return smartResp;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(String id) {
        ModelAndView modelView = new ModelAndView();
        if (StringUtils.isNotEmpty(id)) {
            TNVersion version = versionServ.find(id).getData();
            if (null != version) {
                version.setDescr(unescapeDescr(version.getDescr()));
                modelView.getModelMap().put("objBean", version);
            }
        }
        modelView.setViewName(VIER_DIR + "edit");
        return modelView;
    }

    /**
     * 更新
     *
     * @param version
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public SmartResponse<String> update(TNVersion version) {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != version && StringUtils.isNotEmpty(version.getId())
                && StringUtils.isNotEmpty(version.getVersion())
                && StringUtils.isNotEmpty(version.getDescr())) {
            version.setType(VersionType.PC.getValue());
            version.setVersion(version.getVersion().toUpperCase());
            version.setDescr(escapeDescr(version.getDescr()));
            smartResp = versionServ.update(version);
        }
        return smartResp;
    }

    /**
     * 更新
     *
     * @param version
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public SmartResponse<String> delete(String id) {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (StringUtils.isNotEmpty(id)) {
            smartResp = versionServ.delete(id);
        }
        return smartResp;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @RequestMapping("/view")
    public ModelAndView view(String id) {
        ModelAndView modelView = new ModelAndView();
        if (StringUtils.isNotEmpty(id)) {
            SmartResponse<TNVersion> smartResp = versionServ.find(id);
            modelView.getModelMap().put("smartResp", smartResp);
        }
        modelView.setViewName(VIER_DIR + "view");
        return modelView;
    }

    /**
     * 查看最近更新的10条记录
     *
     * @return
     */
    @RequestMapping("/updateLog")
    public ModelAndView updateLog() {
        ModelAndView modelView = new ModelAndView();
        List<TNVersion> versions = versionServ.getVersion(VersionType.PC, 10);
        modelView.getModelMap().put("versions", versions);
        versions = null;
        modelView.setViewName(VIER_DIR + "updateLog");
        return modelView;
    }

    private String escapeDescr(String descr) {
        descr = StringEscapeUtils.escapeHtml4(descr);
        descr = descr.replace(" ", "&nbsp;");
        descr = descr.replaceAll("\n\r|\n", "<br />");
        return descr;
    }

    private String unescapeDescr(String descr) {
        descr = StringEscapeUtils.unescapeHtml4(descr);
        descr = descr.replaceAll("<br />", "\n");
        descr = descr.replace("&nbsp;", " ");
        return descr;
    }
}
